package com.itheima;

import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        // test1();
        test2();
    }

    private static void test2() {
        TreeMap<Student, String> treeMap = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });

        treeMap.put(new Student("wangwu", 20), "beijing");
        treeMap.put(new Student("zhangsan", 22), "shanghai");
        treeMap.put(new Student("lisi", 21), "guangzhou");
        treeMap.put(new Student("wangwu", 20), "beijing");
        treeMap.forEach((k, v) -> System.out.println(k + " " + v));
    }


    @Test
    public void test1() {
        TreeMap<Student, String> treeMap = new TreeMap<>();
        treeMap.put(new Student("wangwu", 20), "beijing");
        treeMap.put(new Student("zhangsan", 22), "shanghai");
        treeMap.put(new Student("lisi", 21), "guangzhou");
        treeMap.put(new Student("wangwu", 20), "beijing");
        treeMap.forEach((k, v) -> System.out.println(k + " " + v));
    }
}
